﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ex5_1
{
    public partial class Form3 : Form
    {
        static SqlConnection conn = new SqlConnection("Data Source=192.168.30.1;Initial Catalog=STU;Persist Security Info=True;User ID=tt;Password=123456");
        public static string name = Form1.name;
        public static string passwd = Form1.passwd;
        public static string phone = returnSelect("phone");
        public Form3()
        {
            InitializeComponent();
            string name = Form1.name;
            string passwd = Form1.passwd;

            textBox1.Text = name;
            textBox1.ReadOnly = true;

            textBox2.Text = passwd;
            textBox3.Text = phone;
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String passwdt = textBox2.Text;
            string phonet = textBox3.Text;
            string passwdtmp1 = returnSelect("password");
            string phonetmp1 = returnSelect("phone");

            bool passwdEq = passwdt.Equals(passwdtmp1);
            bool phoneEq = phonet.Equals(phonetmp1);

            if (passwdEq&&phoneEq)
            {

            }
            else
            {
                update();
            }
        }
        private void update()
        {
            
            conn.Open();

            // update stuinfo set password='',phone='' where username=""
            SqlCommand cmd = new SqlCommand("set nocount off;update stuinfo set password='" + textBox2.Text + "',phone='" + textBox3.Text + "'where username='"+name+"'", conn);


            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            
            int n;
            try
            {
                n = cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {

                n = 0;
            }

            if (n != 0)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
            conn.Close();
        }
        static String returnSelect(String col)
        {
            conn.Open();
            //select col from stuinfo where username=""
            SqlCommand cmd = new SqlCommand("set nocount off;select "+col+ " from stuinfo where username='" + name + "'", conn);
            SqlDataReader reader = cmd.ExecuteReader();
            string phonenum="";
            while (reader.Read())
            {
                phonenum = reader[col].ToString();
            }



            reader.Close();
            conn.Close();
            return phonenum;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = returnSelect("password");
            textBox3.Text = returnSelect("phone");
        }
    }
    
}
